package com.yongcheon.sms_manager.data.sms_spam;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.CursorAdapter;

import com.yongcheon.sms_manager.vo.SMSVO;

import java.util.ArrayList;

/**
 * Created by yongcheon on 2014-11-27.
 */
public abstract class AbstractSMSData {
    public static String[] reqCols = new String[] { "_id", "thread_id", "address", "date", "protocol", "read", "status", "type", "reply_path_present",
                                                      "subject", "body", "service_center", "locked", "error_code", "seen"};

    public abstract CursorAdapter getCurosrAdapter(Context context, SQLiteDatabase database);
    public abstract void insert(SMSVO vo, Context context, SQLiteDatabase database);
    public abstract void delete(long id, Context context, SQLiteDatabase database);
    public abstract void deleteByPhoneNum(String num, Context context, SQLiteDatabase database);
    public abstract ArrayList<SMSVO> select(String poneNum, Context context, SQLiteDatabase database);

    public SMSVO createSMSVOFromCursor(Cursor cursor){
        SMSVO vo = new SMSVO();
        setSMSVO(vo, "_id",  cursor);
        setSMSVO(vo, "thread_id",  cursor);
        setSMSVO(vo, "address",  cursor);
        setSMSVO(vo, "date",  cursor);
        setSMSVO(vo, "protocol",  cursor);
        setSMSVO(vo, "read",  cursor);
        setSMSVO(vo, "status",  cursor);
        setSMSVO(vo, "type",  cursor);
        setSMSVO(vo, "reply_path_present",  cursor);
        setSMSVO(vo, "subject",  cursor);
        setSMSVO(vo, "body",  cursor);
        setSMSVO(vo, "service_center",  cursor);
        setSMSVO(vo, "locked",  cursor);
        setSMSVO(vo, "error_code",  cursor);
        setSMSVO(vo, "seen",  cursor);
        setSMSVO(vo, "spam_desc",  cursor);
        setSMSVO(vo, "spam_category",  cursor);
        return vo;
    }

    public void setSMSVO(SMSVO vo, String column, Cursor cursor){
        int colIdx = cursor.getColumnIndex(column);
        if(colIdx != -1) {
            long longValue = 0;
            String stringValue = "";
            if(cursor.getType(colIdx) == Cursor.FIELD_TYPE_INTEGER){
                longValue = cursor.getLong(colIdx);
            }else if(cursor.getType(colIdx) == Cursor.FIELD_TYPE_STRING){
                stringValue = cursor.getString(colIdx);
            }

            if(column.equals("_id")) {
                vo.set_Id(longValue);
            }else if(column.equals("sms_id")) {
                vo.setSMS_id(longValue);
            }else if(column.equals("thread_id")) {
                vo.setThread_id(longValue);
            }else if(column.equals("address")) {
                vo.setAddress(stringValue);
            }else if(column.equals("date")) {
                vo.setDate(longValue);
            }else if(column.equals("protocol")) {
                vo.setProtocol(longValue);
            }else if(column.equals("read")) {
                vo.setRead(longValue);
            }else if(column.equals("status")) {
                vo.setStatus(longValue);
            }else if(column.equals("type")) {
                vo.setType(longValue);
            }else if(column.equals("reply_path_present")) {
                vo.setReply_path_present(longValue);
            }else if(column.equals("subject")) {
                vo.setSubject(stringValue);
            }else if(column.equals("body")) {
                vo.setBody(stringValue);
            }else if(column.equals("service_center")) {
                vo.setService_center(stringValue);
            }else if(column.equals("locked")) {
                vo.setLocked(longValue);
            }else if(column.equals("error_code")) {
                vo.setError_code(longValue);
            }else if(column.equals("seen")) {
                vo.setSeen(longValue);
            }else if(column.equals("spam_desc")) {
                vo.setSpam_desc(stringValue);
            }else if(column.equals("spam_category")) {
                vo.setSpam_category(stringValue);
            }
        }
    }

    public ArrayList<SMSVO> getSMSListByNum(Cursor cursor){
        ArrayList<SMSVO> array = new ArrayList<SMSVO>();
        if(cursor != null) {
            while (cursor.moveToNext()) {
                array.add(createSMSVOFromCursor(cursor));
            }
            cursor.close();
        }
        return array;
    }



}
